<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '' }">权限管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/auth/rule' }"
        >权限列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改权限信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form
          :model="form"
          :rules="rules"
          label-width="80px"
          ref="form"
          style=" width: 400px;"
        >
          <el-form-item label="权限名称" prop="ruleName">
            <el-input v-model="form.ruleName"></el-input>
          </el-form-item>
          <el-form-item label="权限码" prop="ruleCode">
            <el-input v-model="form.ruleCode"></el-input>
          </el-form-item>
          <el-form-item label="权限类型" prop="ruleType">
            <el-select placeholder="请选择权限类型" v-model="form.ruleType">
              <el-option
                :key="item.code"
                :label="item.desc"
                :value="item.code"
                v-for="item in ruleTypeList"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="权限条件" prop="ruleCondition">
            <el-input v-model="form.ruleCondition"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即提交
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        ruleName: "",
        ruleCode: "",
        ruleType: "",
        ruleCondition: "",
        remark: null
      },
      rules: {
        ruleName: [
          { required: true, message: "请输入权限名", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        ruleCode: [
          { required: true, message: "请输入权限码", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        ruleType: [
          { required: true, message: "请选择权限类型", trigger: "none" }
        ]
      },
      ruleTypeList: [
        { code: 1, desc: "页面" },
        { code: 2, desc: "接口" },
        { code: 3, desc: "数据" }
      ]
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
  },
  methods: {
    info() {
      this.$ajax.auth_rule
        .info(this.id)
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
            this.$router.push("/auth/rule");
          } else {
            this.form = result.data;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.auth_rule
            .update(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "提交成功!"
                });
                this.$router.push("/auth/rule");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/auth/rule");
    }
  }
};
</script>
